package com.zbkj.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.card.StoreGiftCard;
import com.zbkj.common.model.category.Category;
import com.zbkj.common.model.product.StoreProduct;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.SearchAndPageRequest;
import com.zbkj.common.request.StoreGiftCardRequest;
import com.zbkj.common.request.StoreGiftCardSearchRequest;
import com.zbkj.common.response.StoreGiftCardInfoResponse;
import com.zbkj.common.utils.CrmebUtil;
import com.zbkj.common.vo.GiftCardExportVo;
import com.zbkj.service.dao.StoreGiftCardDao;
import com.zbkj.service.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 礼品卡模版 服务实现
 *
 * @author Swallows
 * @date 2022-08-17 14:34
 */
@Service
@RequiredArgsConstructor
public class StoreGiftCardServiceImpl extends ServiceImpl<StoreGiftCardDao, StoreGiftCard> implements StoreGiftCardService {

    private final StoreGiftCardDao dao;
    private final StoreGiftCardUserService giftCardUserService;
    private final StoreProductService storeProductService;
    private final CategoryService categoryService;
    private final ExcelService excelService;

    @Override
    @SuppressWarnings("unchecked")
    public List<StoreGiftCard> getPageList(StoreGiftCardSearchRequest request, PageParamRequest pageParamRequest) {

        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        LambdaQueryWrapper<StoreGiftCard> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StoreGiftCard::getIsDel, false);
        lqw.eq(ObjectUtil.isNotNull(request.getCardType()), StoreGiftCard::getCardType, request.getCardType());
        lqw.eq(ObjectUtil.isNotNull(request.getUseType()), StoreGiftCard::getUseType, request.getUseType());
        lqw.eq(ObjectUtil.isNotNull(request.getStatus()), StoreGiftCard::getStatus, request.getStatus());
        lqw.like(StrUtil.isNotBlank(request.getName()), StoreGiftCard::getName, request.getName());
        lqw.orderByDesc(StoreGiftCard::getSort, StoreGiftCard::getId);
        return dao.selectList(lqw);
    }

    @Override
    public boolean delById(Integer id) {

        StoreGiftCard giftCard = getById(id);
        checkException(giftCard);

        giftCard.setIsDel(true);
        return dao.updateById(giftCard) > 0;
    }

    @Override
    public boolean saveByRequest(StoreGiftCardRequest request) {

        StoreGiftCard giftCard = new StoreGiftCard();
        BeanUtil.copyProperties(request, giftCard);

        if (!request.getIsFixedTime()) {
            if (giftCard.getDay() == null || giftCard.getDay() == 0) {
                throw new CrmebException("请输入天数！");
            }
            giftCard.setUseStartTime(null);
            giftCard.setUseEndTime(null);
        }
        return save(giftCard);
    }

    @Override
    public List<StoreGiftCard> getSendList(SearchAndPageRequest request) {
        return null;
    }

    @Override
    public boolean updateStatusById(Integer id, Boolean status) {

        StoreGiftCard giftCard = getById(id);
        checkException(giftCard);

        if (giftCard.getStatus().equals(status)) {
            throw new CrmebException("状态一致无需变更");
        }

        StoreGiftCard card = new StoreGiftCard();
        card.setId(id);
        card.setStatus(status);

        return updateById(card);
    }

    @Override
    public String makeCard(Integer id, Integer nums) {

        StoreGiftCard giftCard = getById(id);
        checkException(giftCard);

        List<GiftCardExportVo> exportList = giftCardUserService.makeCards(giftCard, nums);
        if (exportList.size() < 1) {
            throw new CrmebException("制卡失败,无可导出数据");
        }
        return excelService.exportGiftCard(exportList);
    }

    @Override
    public StoreGiftCardInfoResponse getInfoById(Integer id) {

        StoreGiftCard giftCard = getById(id);
        checkException(giftCard);

        List<StoreProduct> productList = null;
        List<Category> categoryList = null;
        if (StrUtil.isNotBlank(giftCard.getPrimaryKey()) && giftCard.getUseType() > 1) {
            List<Integer> primaryKeys = CrmebUtil.stringToArray(giftCard.getPrimaryKey());
            if (giftCard.getUseType() == 2) {
                productList = storeProductService.getListInIds(primaryKeys);
            } else if (giftCard.getUseType() == 3) {
                categoryList = categoryService.getByIds(primaryKeys);
            }
        }

        StoreGiftCardRequest request = new StoreGiftCardRequest();
        BeanUtil.copyProperties(giftCard, request);

        return new StoreGiftCardInfoResponse(request, productList, categoryList);
    }

    /**
     * 异常校验
     */
    private void checkException(StoreGiftCard giftCard) {

        if (giftCard == null || giftCard.getIsDel()) {
            throw new CrmebException("礼品卡不存在或者已经失效");
        }
    }
}
